package com.sgroup.studycountry.support.rest;

import java.io.Serializable;

/**
 * @author umi
 * @date 2021/10/27 19:43
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private T data;
    private boolean success;

    protected R() {
    }

    private R(int code, String msg, T data, boolean success) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    private R(int code, String msg, T data) {
        this(code, msg, data, RestCode.SUCCESS.code == code);
    }

    public static <T> R<T> data(T data) {
        return new R(RestCode.SUCCESS.code, RestCode.SUCCESS.msg, data);
    }

    public static <T> R<T> data(T data, String msg) {
        return new R(RestCode.SUCCESS.code, msg, data);
    }

    public static <T> R<T> data(T data, int code, String msg) {
        return new R(code, msg, data, true);
    }

    public static R<?> ok() {
        return new R(RestCode.SUCCESS.code, RestCode.SUCCESS.msg, (Object)null);
    }

    public static R<?> ok(String msg) {
        return new R(RestCode.SUCCESS.code, msg, (Object)null);
    }

    public static R<?> ok(IRestCode rc) {
        return new R(rc.getCode(), rc.getMsg(), (Object)null, true);
    }

    public static R<?> fail() {
        return new R(RestCode.FAILURE.code, RestCode.FAILURE.msg, (Object)null);
    }

    public static R<?> fail(String msg) {
        return new R(RestCode.FAILURE.code, msg, (Object)null);
    }

    public static R<?> fail(IRestCode rc) {
        return new R(rc.getCode(), rc.getMsg(), (Object)null, false);
    }

    public static <T> R<T> fail(T data) {
        return fail(data, RestCode.FAILURE.msg);
    }

    public static <T> R<T> fail(T data, String msg) {
        return fail(data, msg, RestCode.FAILURE.code);
    }

    public static <T> R<T> fail(T data, String msg, int code) {
        return new R(code, msg, data, false);
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String toString() {
        return "R{code=" + this.code + ", msg='" + this.msg + '\'' + ", data=" + this.data + ", success=" + this.success + '}';
    }
}